/**
 * Stat.java - 2012-5-21
 *
 * Licensed Property to China UnionPay Co., Ltd.
 * 
 * (C) Copyright of China UnionPay Co., Ltd. 2010
 *     All Rights Reserved.
 *
 * Project Info: China UnionPay Internet Acquiring Project
 * 
 * Modification History:
 * =============================================================================
 *   Author         Date          Description
 *   ------------ ---------- ---------------------------------------------------
 *        
 * =============================================================================
 */
package com.lijin.study.java.cardstat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Description: 
 *
 * (C) Copyright of China UnionPay Co., Ltd. 2010.
 *
 */
public class Stat {

	private static String FILE_NM_CI = "D:\\TBL_UPONL_CARD_INF.txt";
	private static String FILE_NM_BR = "D:\\TBL_UPONL_BIND_RELATION.txt";
	private static String FILE_NM_BI = "D:\\TBL_UPONL_BIN.txt";
	private static String FILE_NM_IS = "D:\\TBL_UPONL_INS_BAS.txt";
	
	private static String FILE_NM_RST = "D:\\RESULT.txt";
	
	private static Map<String, Map<String, String>> insMap = new HashMap<String, Map<String, String>>();
	private static Map<String, Map<String, String>> binInsMap = new HashMap<String, Map<String, String>>();
	
	public static void main(String[] args) throws Exception {
		
//		initInsMap();
//		initBinInsMap();
		cntCardInf();
	}
	
	public static void cntCardInf() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(FILE_NM_RST))));
//		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(FILE_NM_RST))));
		
		int c = 0;
		String line = null;
		Set<String> cards = new HashSet<String>();
		System.out.println(Runtime.getRuntime().maxMemory());
		while (reader.ready()) {
			line = reader.readLine().trim();
			c ++;
			if (!cards.contains(line)) {
				cards.add(line);
			}
		}
		System.out.println(cards.size());
		reader.close();
	}
	
	public static void buildAll() throws Exception {
		Map<String, Map<String, Integer>> result = new HashMap<String, Map<String, Integer>>();
		
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\RESULT_kuaijie_20120101_after.txt"))));
		String line = null;
		while(reader.ready()) {
			line = reader.readLine().trim();
			String[] strs = line.split("[|]");
			String insCd = strs[0];
			Map<String, Integer> m = null;
			if (!"01".equals(strs[1]) && !"02".equals(strs[1])) {
				continue;
			}
			String k = "kuaijie_20120101_after_" + strs[1];
			Integer v = Integer.valueOf(strs[2]);
			if (result.containsKey(insCd)) {
				m = result.get(insCd);
				if (m.get(k) != null) {
					m.put(k, m.get(k) + v);
				} else {
					m.put(k, v);
				}
			} else {
				m = new HashMap<String, Integer>();
				m.put(k, v);
			}
			result.put(insCd, m);
		}
		reader.close();
		
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\RESULT_kuaijie_20120101_before.txt"))));
		line = null;
		while(reader.ready()) {
			line = reader.readLine().trim();
			String[] strs = line.split("[|]");
			String insCd = strs[0];
			Map<String, Integer> m = null;
			if (!"01".equals(strs[1]) && !"02".equals(strs[1])) {
				continue;
			}
			String k = "kuaijie_20120101_before_" + strs[1];
			Integer v = Integer.valueOf(strs[2]);
			if (result.containsKey(insCd)) {
				m = result.get(insCd);
				if (m.get(k) != null) {
					m.put(k, m.get(k) + v);
				} else {
					m.put(k, v);
				}
			} else {
				m = new HashMap<String, Integer>();
				m.put(k, v);
			}
			result.put(insCd, m);
		}
		reader.close();
		
		
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\RESULT_renzheng_20120101_after.txt"))));
		line = null;
		while(reader.ready()) {
			line = reader.readLine().trim();
			String[] strs = line.split("[|]");
			String insCd = strs[0];
			Map<String, Integer> m = null;
			if (!"01".equals(strs[1]) && !"02".equals(strs[1])) {
				continue;
			}
			String k = "renzheng_20120101_after_" + strs[1];
			Integer v = Integer.valueOf(strs[2]);
			if (result.containsKey(insCd)) {
				m = result.get(insCd);
				if (m.get(k) != null) {
					m.put(k, m.get(k) + v);
				} else {
					m.put(k, v);
				}
			} else {
				m = new HashMap<String, Integer>();
				m.put(k, v);
			}
			result.put(insCd, m);
		}
		reader.close();
		
		
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\RESULT_renzheng_20120101_before.txt"))));
		line = null;
		while(reader.ready()) {
			line = reader.readLine().trim();
			String[] strs = line.split("[|]");
			String insCd = strs[0];
			Map<String, Integer> m = null;
			if (!"01".equals(strs[1]) && !"02".equals(strs[1])) {
				continue;
			}
			String k = "renzheng_20120101_before_" + strs[1];
			Integer v = Integer.valueOf(strs[2]);
			if (result.containsKey(insCd)) {
				m = result.get(insCd);
				if (m.get(k) != null) {
					m.put(k, m.get(k) + v);
				} else {
					m.put(k, v);
				}
			} else {
				m = new HashMap<String, Integer>();
				m.put(k, v);
			}
			result.put(insCd, m);
		}
		reader.close();
		
		
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\RESULT_xiaoe_20120101_after.txt"))));
		line = null;
		while(reader.ready()) {
			line = reader.readLine().trim();
			String[] strs = line.split("[|]");
			String insCd = strs[0];
			Map<String, Integer> m = null;
			if (!"01".equals(strs[1]) && !"02".equals(strs[1])) {
				continue;
			}
			String k = "xiaoe_20120101_after_" + strs[1];
			Integer v = Integer.valueOf(strs[2]);
			if (result.containsKey(insCd)) {
				m = result.get(insCd);
				if (m.get(k) != null) {
					m.put(k, m.get(k) + v);
				} else {
					m.put(k, v);
				}
			} else {
				m = new HashMap<String, Integer>();
				m.put(k, v);
			}
			result.put(insCd, m);
		}
		reader.close();
		
		
		
		reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\RESULT_xiaoe_20120101_before.txt"))));
		line = null;
		while(reader.ready()) {
			line = reader.readLine().trim();
			String[] strs = line.split("[|]");
			String insCd = strs[0];
			Map<String, Integer> m = null;
			if (!"01".equals(strs[1]) && !"02".equals(strs[1])) {
				continue;
			}
			String k = "xiaoe_20120101_before_" + strs[1];
			Integer v = Integer.valueOf(strs[2]);
			if (result.containsKey(insCd)) {
				m = result.get(insCd);
				if (m.get(k) != null) {
					m.put(k, m.get(k) + v);
				} else {
					m.put(k, v);
				}
			} else {
				m = new HashMap<String, Integer>();
				m.put(k, v);
			}
			result.put(insCd, m);
		}
		System.out.println(result);
		reader.close();
		
		
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(FILE_NM_RST))));
		for (String key : result.keySet()) {
			StringBuilder sb = new StringBuilder();
			Map<String, Integer> vm = result.get(key);
			String insNm = insMap.get(key).get("insNm");
			String kuaijie_20120101_before_01 = vm.get("kuaijie_20120101_before_01") == null ? "0" : String.valueOf(vm.get("kuaijie_20120101_before_01"));
			String kuaijie_20120101_before_02 = vm.get("kuaijie_20120101_before_02") == null ? "0" : String.valueOf(vm.get("kuaijie_20120101_before_02"));

			String kuaijie_20120101_after_01 = vm.get("kuaijie_20120101_after_01") == null ? "0" : String.valueOf(vm.get("kuaijie_20120101_after_01"));
			String kuaijie_20120101_after_02 = vm.get("kuaijie_20120101_after_02") == null ? "0" : String.valueOf(vm.get("kuaijie_20120101_after_02"));
			
			String renzheng_20120101_before_01 = vm.get("renzheng_20120101_before_01") == null ? "0" : String.valueOf(vm.get("renzheng_20120101_before_01"));
			String renzheng_20120101_before_02 = vm.get("renzheng_20120101_before_02") == null ? "0" : String.valueOf(vm.get("renzheng_20120101_before_02"));

			String renzheng_20120101_after_01 = vm.get("renzheng_20120101_after_01") == null ? "0" : String.valueOf(vm.get("renzheng_20120101_after_01"));
			String renzheng_20120101_after_02 = vm.get("renzheng_20120101_after_02") == null ? "0" : String.valueOf(vm.get("renzheng_20120101_after_02"));
			
			String xiaoe_20120101_before_01 = vm.get("xiaoe_20120101_before_01") == null ? "0" : String.valueOf(vm.get("xiaoe_20120101_before_01"));
			String xiaoe_20120101_before_02 = vm.get("xiaoe_20120101_before_02") == null ? "0" : String.valueOf(vm.get("xiaoe_20120101_before_02"));

			String xiaoe_20120101_after_01 = vm.get("xiaoe_20120101_after_01") == null ? "0" : String.valueOf(vm.get("xiaoe_20120101_after_01"));
			String xiaoe_20120101_after_02 = vm.get("xiaoe_20120101_after_02") == null ? "0" : String.valueOf(vm.get("xiaoe_20120101_after_02"));
			
			
			sb.append(key).append("|")
				.append(insNm).append("|")
				.append(kuaijie_20120101_before_01).append("|")
				.append(kuaijie_20120101_before_02).append("|")
				.append(kuaijie_20120101_after_01).append("|")
				.append(kuaijie_20120101_after_02).append("|")
				.append(renzheng_20120101_before_01).append("|")
				.append(renzheng_20120101_before_02).append("|")
				.append(renzheng_20120101_after_01).append("|")
				.append(renzheng_20120101_after_02).append("|")
				.append(xiaoe_20120101_before_01).append("|")
				.append(xiaoe_20120101_before_02).append("|")
				.append(xiaoe_20120101_after_01).append("|")
				.append(xiaoe_20120101_after_02);
			writer.write(sb.toString());
			writer.newLine();
		}
		writer.close();
	}
	
	public static void delCardInf() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(FILE_NM_CI))));
		int c = 0;
		String line = null;
		Map<String, Integer> cntMap = new HashMap<String, Integer>();
		while (reader.ready()) {
			line = reader.readLine();
			c ++;
			if (c >= 4 && line.length() > 600) {
				String recUpdTs = line.substring(281, 291).replaceAll("-", "");
				if (Integer.valueOf(recUpdTs) >= 20120101 && Integer.valueOf(recUpdTs) <= 20120430) {
					String ctrl1 = line.substring(210, 211);
					if ("2".equals(ctrl1) || "3".equals(ctrl1)) {
						String acctNo = line.substring(12, 32).trim();
						int binLen = 6;
						String insCd = null;
						String cardAttr = null;
						String len = String.valueOf(acctNo.length());
						while (insCd == null || insCd.length() == 0) {
							String bin = acctNo.substring(0, binLen);
							Map<String, String> binMap = binInsMap.get(len + bin);
							if (binMap != null) {
								insCd = binMap.get("insCd");
								cardAttr = binMap.get("cardAttr");
								break;
							}
							binLen ++;
							if (binLen == 10) {
								break;
							}
						}
						if (insCd != null && insCd.length() > 0) {
							Map<String, String> insInf = insMap.get(insCd);
							if (insInf != null) {
								String headIns = insInf.get("headIns");
								String key = headIns + "_" + cardAttr;
								if (cardAttr == null) {
									System.out.println(headIns);
									System.out.println(line);
								}
								if (cntMap.get(key) != null) {
									cntMap.put(key, cntMap.get(key) + 1);
								} else {
									cntMap.put(key, 1);
								}
							}
						}
					}
				}
			}
		}
		reader.close();
		
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(FILE_NM_RST))));
		for (String key : cntMap.keySet()) {
			StringBuilder sb = new StringBuilder();
			String bankNo = key.split("_")[0];
			String cardAttr = key.split("_")[1];
			sb.append(bankNo).append("|")
				.append(cardAttr).append("|")
				.append(cntMap.get(key));
			writer.write(sb.toString());
			writer.newLine();
		}
		writer.close();
	}
	
	public static void dealBr() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(FILE_NM_BR))));
		String line = null;
		int c = 0;
		Map<String, Integer> cntMap = new HashMap<String, Integer>();
		while (reader.ready()) {
			line = reader.readLine();
			c ++;
			if (c >= 4 && line.length() > 300) {
				String resSt = line.substring(323, 324);
				if ("1".equals(resSt)) {
					String bankNo = line.substring(116, 126).trim();
					String cardAttr = line.substring(90, 92).trim();
					String recCrtTs = line.substring(229, 239).replaceAll("-", "");
					int t = Integer.valueOf(recCrtTs);
					if (t >= 20120101 && t <= 20120430) {
						String key = bankNo + "_" + cardAttr;
						if (cntMap.get(key) != null) {
							cntMap.put(key, cntMap.get(key) + 1);
						} else {
							cntMap.put(key, 1);
						}
					}
				}
			}
		}
		reader.close();
		
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(FILE_NM_RST))));
		
		for (String key : cntMap.keySet()) {
			StringBuilder sb = new StringBuilder();
			String bankNo = key.split("_")[0];
			String cardAttr = key.split("_")[1];
			sb.append(bankNo).append("|")
				.append(cardAttr).append("|")
				.append(cntMap.get(key));
			writer.write(sb.toString());
			writer.newLine();
		}
		
		writer.close();
	}
	
	private static void initInsMap() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(FILE_NM_IS)), "GBK"));
		String line = null;
		int c = 0;
		while (reader.ready()) {
			line = reader.readLine();
			byte[] arr = line.getBytes("GBK");
			c ++;
			if (c >= 4 && arr.length > 300) {
				Map<String, String> insInf = new HashMap<String, String>();
				String insCd = new String(arr, 0, 10).trim();
				insInf.put("headIns", new String(arr, 28, 10).trim());
				insInf.put("insNm", new String(arr, 83, 60, "GBK").trim());
				insMap.put(insCd, insInf);
			}
		}
		reader.close();
	}
	
	private static void initBinInsMap() throws Exception {
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(FILE_NM_BI)), "GBK"));
		String line = null;
		int c = 0;
		while (reader.ready()) {
			line = reader.readLine();
			byte[] arr = line.getBytes("GBK");
			c ++;
			if (c >= 4 && arr.length > 300) {
				Map<String, String> binInf = new HashMap<String, String>();
				String bin = new String(arr, 0, 14).trim();
				String insCd = new String(arr, 87, 11).trim();
				String cardAttr = new String(arr, 146, 2);
				binInf.put("insCd", insCd);
				binInf.put("cardAttr", cardAttr);
				binInsMap.put(bin, binInf);
			}
		}
	}
}
